package com.hshx.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hshx.pojo.Dept;
import com.hshx.pojo.Role;
import com.hshx.pojo.Users;
import com.hshx.service.IMessageProduceService;
import com.hshx.service.RoleService;
import com.hshx.service.UsersService;
import com.hshx.util.RedisUtil1;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@CrossOrigin
@RequestMapping("/systemService")
@RestController
public class RoleController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private RedisUtil1 redisUtil1;

    /**
     * 得到所有的角色信息
     * @return
     */
    @RequestMapping("/getAllRole")
    public List<Role> getAllRole(){
        return roleService.list();
    }

    /**
     * 新增一个角色
     * @param role
     * @return
     */
    @RequestMapping("/addRole")
    public boolean addRole(@SpringQueryMap Role role){
        if(roleService.save(role)){
            flushRoleRedis();
            return true;
        }
        return false;
    }

    /**
     * 修改某个角色的信息
     * @param role
     * @return
     */
    @RequestMapping("/updateRole")
    public boolean updateRole(@SpringQueryMap Role role){
        if(usersService.count(new QueryWrapper<Users>().eq("users_role",role.getRole_id()))>0&&role.getRole_state()!=-1){
            return false;
        }
        if(roleService.updateById(role)){
            flushRoleRedis();
            return true;
        }
        return false;
    }

    /**
     * 从数据库中移除一条角色记录
     * @param role_id
     * @return
     */
    @RequestMapping("/removeRole")
    public boolean removeRole(int role_id){
        if(usersService.count(new QueryWrapper<Users>().eq("users_role",role_id))>0){
            return false;
        }
        if(roleService.removeById(role_id)){
            flushRoleRedis();
            return true;
        }
        return false;
    }


    /**
     * 得到某一条角色信息
     */
    @RequestMapping("/getRoleOne")
    public Role getRoleOne(int role_id){
        return roleService.getById(role_id);
    }


    /**
     * 得到启用的角色集合
     * @return
     */
    @RequestMapping("/getRoleList")
    public List<Role> getRoleList(){
        List<Object> objects = redisUtil1.lRange("RoleRedisList", 0, -1);
        if(objects!=null&&objects.size()>0) {
            return objects.stream().map(o -> ((Role) o)).collect(Collectors.toList());
        }
        List<Role> roleList=roleService.list(new QueryWrapper<Role>().eq("role_state",1));
        redisUtil1.lPushAll("RoleRedisList",roleList,60L*24);
        return roleList;
    }

    /**
     * 得到所有的角色信息
     * @return
     */
    @RequestMapping("/getRolePage")
    public Map<String,Object> getRolePage(Integer pageIndex, Integer pageSize){
        HashMap<String, Object> roleMap = new HashMap<>();
        if(pageIndex==null||pageIndex<=0){
            pageIndex=1;
        }
        if(pageSize==null||pageSize<=0){
            pageSize=10;
        }
        List<Object> objects = redisUtil1.lRange("roleAll",0,-1);
        if(objects!=null&&objects.size()>0){
            System.out.println("redis中存在");

            List<Role> roleList= objects.stream().map(o->((Role)o)).collect(Collectors.toList());
            roleMap.put("pageSize",pageSize);
            roleMap.put("pages", roleList.size()%pageSize==0?roleList.size()/pageSize:roleList.size()/pageSize+1);
            roleMap.put("pageNum",pageIndex);
            roleMap.put("list", roleList.stream().sorted(Comparator.comparing(Role::getRole_id).reversed()).skip((pageIndex-1)*pageSize).limit(pageSize).collect(Collectors.toList()));
            roleMap.put("total", roleList.size());
        }else{//redis中不存在或失效
            System.out.println("redis中不存在或失效");

            Page rolePage= PageHelper.startPage(pageIndex,pageSize);
            List<Role> roleList=roleService.list(new QueryWrapper<Role>().orderByDesc("role_id"));
            redisUtil1.lPushAll("roleAll",roleList,24*60L);
            roleMap.put("pageSize",rolePage.getPageSize());
            roleMap.put("pages", rolePage.getPages());
            roleMap.put("pageNum",rolePage.getPageNum());
            roleMap.put("list", rolePage.getResult());
            roleMap.put("total", rolePage.getTotal());
        }
        return roleMap;
    }


    @RequestMapping("/hasName")
    public boolean hasName(String roleName){
        return roleService.count(new QueryWrapper<Role>().eq("role_name", roleName))==0;
    }


    /**
     * 1.更新redis中启用角色的信息
     * 2.更新redis中所有的角色信息
     */
    private void flushRoleRedis(){
        redisUtil1.remove("roleAll");
        redisUtil1.lPushAll("roleAll",roleService.list(new QueryWrapper<Role>().orderByDesc("role_id")),60L*24);
        redisUtil1.remove("RoleRedisList");
        redisUtil1.lPushAll("RoleRedisList",roleService.list(new QueryWrapper<Role>().eq("role_state",1)),60L*24);
    }



}
